<?php

/* 
 * 采集bbs
 * 2016、01、20 wzl
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
class storyController extends AppController
{
    public function __construct() {
        parent::__construct();
    }
    public function source(){
        $this->title = '来源';
        $this->_get_source_data();
        $this->display('source_list');
    }
    public function source_query(){
        $this->_get_source_data();
        $this->ret['status'] = 0;
        $this->ret['content'] = $this->fetch('source_list');
        exit(json_encode($this->ret));
    }

    public function sourece_create(){
        $this->create_token();
        $this->get_cookie();
        $this->title = '添加来源';
        $this->display('sourece_create');
    }
    public function sourece_new_create(){
        $this->check_token();
        $data = array(
            'uname' => trim($_POST['uname']),
            'url' => trim($_POST['url']),
        );
        $this->set_cookie($data);
        if(empty($data['uname'])) {
            $this->show_msg('名称不能为空');
        }
        if(empty($data['url'])) {
            $this->show_msg('URL不能为空');
        }
        
    }
    public function sourece_del(){
        
    }
    /**
     * 来源获取数据
     */
    private function _get_source_data(){
        $page = isset($_REQUEST['page']) ? $_REQUEST['page'] : 1;  
        $filter['sname'] = isset($_POST['sname']) ? $_POST['sname'] : '';
        if(!empty($filter['user_name'])) {
            $where .= ' and instr(sname, ' . "'{$filter['sname']}')";
        }
        $key = storySourceModel::load()->keyFields;
        $sort_by = isset($_REQUEST['sort_by']) ? $_REQUEST['sort_by'] : $key;
        $sort_order = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : 'DESC';
        $pageObj = new pageModel('story_source', $where, $this->page_number, 10, $sort_by, $sort_order);        
        $data = $pageObj->pageData($page);
        $list = $data['pageData'];
        if($list) {
            foreach ($list as $key => $row) {                
                $data['pageData'][$key]['id'] = $key + 1;
                $data['pageData'][$key]['in_time_format'] = publicController::load()->friendlyDate($row['in_time']);               
                $data['pageData'][$key]['is_show'] = $row['is_show'] == 1 ? 'glyphicon-ok' : 'glyphicon-remove';  
            }
        }
        $this->assign('sort_' . $sort_by, $data['sort_img']);
        $this->assign('data', $data);
    }
    public function get(){
        $this->display('get_story');
    }
    public function doget(){
        $url = trim($_POST['url']);
        if($url) {
            $data = file_get_contents($url);
            //判断是什么编码
            $char = mb_detect_encoding($data,array('ASSCII','GB2312','UTF-8'));
            if($char != 'UTF-8') {
                $data = iconv($char, "utf-8", $data);     
            }            
            if($data) {
                echo $this->setReturn(0, $data);exit;
            }
        }
        echo $this->setReturn(100, 'no msg');exit;
    }
    public function news(){
        $file = 'http://bbs.tianya.cn/post-no05-408025-1.shtml';
        phpQuery::newDocumentFile($file);
        $bd = pq('#bd');
        echo "<pre>";
        echo $bd->find(".s_title")->html() . '<br/>';
//        echo $bd->find(".host-item")->find(".bbs-content")->html();
        echo $bd->find("#post_head")->find(".atl-info a:eq(0)")->attr("uid");
        echo $bd->find("#post_head")->find(".atl-info a:eq(0)")->attr("uname");
        $maindiv = $bd->find(".atl-item");
//        echo $maindiv;
        foreach ($maindiv as $key => $row) {
            if($key == 1)continue;
            $_replyid = pq($row)->attr("replyid");
            $_date = pq($row)->attr('js_restime');
            $_author = urldecode(pq($row)->attr("js_username"));
//            echo pq($row)->find(".bbs-content")->html();
        }
    }
}
